package com.yazi.mxz.behavior.controller;


import javax.annotation.Resource;

import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.behavior.common.BaseController;
import com.yazi.mxz.behavior.entity.repository.StoreGoods;
import com.yazi.mxz.behavior.jackson.filter.StoreGoodsFilter;
import com.yazi.mxz.behavior.repository.StoreGoodsRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.TextCriteria;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("storeGoodsController")
@RequestMapping("/storeGoods")
public class StoreGoodsController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreGoodsController.class);

	@Resource
	StoreGoodsRepository storeGoodsRepository;
	
	
	@RequestMapping(value="/list/json",params={"searchWord"})
	@ResponseBody
	public ObjectResult<?> listJson(String searchWord, Pageable pb) {
		return list(searchWord, null, null, null, null, pb);		
	}
	
	@RequestMapping(value="/list/json",params={"storeId"})
	@ResponseBody
	public ObjectResult<?> listJson(long storeId, Pageable pb) {
		return list(null, storeId, null, null, null, pb);
	}
	
	@RequestMapping(value="/list/json",params={"storeId", "searchWord"})
	@ResponseBody
	public ObjectResult<?> listJson(long storeId, String searchWord, Pageable pb) {
		return list(searchWord, storeId, null, null, null, pb);
	}
	
	@RequestMapping(value="/list/json",params={"storeId", "classifyId", "deep"})
	@ResponseBody
	public ObjectResult<?> listJson(long storeId, long classifyId, int deep, Pageable pb) {
		return list(null, storeId, classifyId, null, deep, pb);
	}
	
	@RequestMapping(value="/list/json",params={"catId", "deep"})
	@ResponseBody
	public ObjectResult<?> listJson(long catId, int deep, Pageable pb) {
		return list(null, null, null, catId, deep, pb);		
	}
	
	private ObjectResult<?> list(String searchWord, Long storeId, Long classifyId, Long catId, Integer deep, Pageable pb) {

		ObjectResult<Page<StoreGoods>> jsonResult = new ObjectResult<Page<StoreGoods>>();
		jsonResult.setFilter(StoreGoodsFilter.get());
		Page<StoreGoods> page = null;
		try {
			if (null != searchWord && !searchWord.isEmpty()) {
				TextCriteria tc = TextCriteria.forDefaultLanguage().matching(searchWord);
				if (null != storeId) {
					page = storeGoodsRepository.findByStore_StoreId(storeId, tc, pb);
				} else {
					page = storeGoodsRepository.findAllBy(tc, pb);
				}
			} else if (null != storeId) {
				if (null != classifyId) {
					if (deep == 0) {
						page = storeGoodsRepository.findByStore_StoreIdAndClassifyId0(storeId, classifyId, pb);
					} else if (deep == 1) {
						page = storeGoodsRepository.findByStore_StoreIdAndClassifyId1(storeId, classifyId, pb);
					}
				} else {
					page = storeGoodsRepository.findByStore_StoreId(storeId, pb);
				}					
			} else if (null != catId) {
				if (deep == 1) {
					page = storeGoodsRepository.findByCatId0(catId, pb);
				} else if (deep == 2) {
					page = storeGoodsRepository.findByCatId1(catId, pb);
				} else if (deep == 3) {
					page = storeGoodsRepository.findByCatId2(catId, pb);
				}
			} else {
				page = storeGoodsRepository.findAll(pb);
			}
								
			jsonResult.setResult(page);
			jsonResult.setOk();
			
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;

	}
}
